﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using Ionic.Zip;

namespace ShadowEraDeckEditor
{
    public partial class FormDevTools : Form
    {
        public FormDevTools()
        {
            InitializeComponent();
        }

        private void FormUpdate_Load(object sender, EventArgs e)
        {
            this.Location = Owner.Location;
        }

        private void deckEditorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Owner.Location = this.Location;
            ((FormMenu)Owner).nextform = 1;
            this.Close();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ((FormMenu)Owner).nextform = 5;
            this.Close();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            
            //Create a new subfolder under the current active folder
            string appPath = Path.GetDirectoryName(Application.ExecutablePath);
            appPath = System.IO.Path.Combine(appPath, "images");
            // Create the subfolder
            System.IO.Directory.CreateDirectory(appPath);
            // Combine the new file name with the path
            //newPath = System.IO.Path.Combine(newPath, newFileName);
            int i;
            int.TryParse(textBoxCount.Text, out i);
            labelTotCount.Text = i.ToString();
            i++;
            for (int x = 1; x < i; x++)
            {
                string currentcard = "se";
                if (x < 100)
                    currentcard += "0";
                if (x < 10)
                    currentcard += "0";
                currentcard += x + ".jpg";
                string tempPath = appPath + "\\" + currentcard;
                string tempURL = "http://www.shadowera.com/cards/" + currentcard;

                //labelCurCount.Text = x.ToString();
                WebClient mywebClient = new WebClient();
                //mywebClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
                mywebClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
                //mywebClient.DownloadFileAsync(new Uri("http://www.shadowera.com/cards/se180.jpg"), @"c:\images\myfile.jpg");
                mywebClient.DownloadFileAsync(new Uri(tempURL), tempPath);
                mywebClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(File.Exists("carddb.dat"))
                File.Delete("carddb.dat");
            CreatePak();
            MessageBox.Show("New .dat file created.");
        }

        public void CreatePak()
        {
            if (!File.Exists("carddb.dat"))
            {
                using (ZipFile zip = new ZipFile())
                {
                    zip.CompressionLevel = Ionic.Zlib.CompressionLevel.None;
                    zip.Password = "c@n7cr4kD1zSh!t";
                    zip.AddDirectory("images", "images");
                    zip.AddFile("carddb.ini");
                    zip.AddFile("carddb.version.ini");
                    zip.Save("carddb.dat");
                }
            }
        }
        
        #region useless progress bar stuff
        private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

        private void Completed(object sender, AsyncCompletedEventArgs e)
        {
            //MessageBox.Show("Download completed!");
            int i;
            int.TryParse(labelCurCount.Text, out i);
            i++;
            labelCurCount.Text = i.ToString();
            int a;
            int.TryParse(labelTotCount.Text, out a);
            if (i == a)
                MessageBox.Show("Picture downloads completed.");
        }
        #endregion
    }
}
